package org.brisling.oa.queryService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.oa.domain.OADutyList;
import org.brisling.sysman.domain.UserPassword;
import org.brisling.sysman.queryService.QueryDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/oaDutyList/query")
public class QueryOADutyListService extends AbstractBaseQueryService<OADutyList> {
	
	//自动匹配登录验证服务类
	@Autowired
	private ShiroSecurityService shiroSecurityService;
	
	@Autowired
	private QueryDictionaryService dicService;
	public QueryOADutyListService(){
		this.moduleName = ConstSysParam.MODULE_OA_DUTY_LIST;
		this.subSystem = ConstSysParam.SUBSYSTEM_OA;
		this.tableName = this.moduleName;
		this.clazz = OADutyList.class;
		
		typeMap = this.getTypeMap(clazz);
		
		
		orderMap.put("sortedid","asc");			
		
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return this.getBizEditPage();
	}
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<OADutyList> getListDomain(@RequestBody(required=false) String body){	
		
		return super.getListDomainByConditionAndOrder(body, typeMap, orderMap,true);
	}
	
	@RequestMapping("/getListDomainByDeptid.action")
	public ListDomain<OADutyList> getListDomainByDeptid(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="deptid",required=false) Integer deptid,
			@RequestParam(value="sort",required=false) String sort){		
		
		if(deptid==null){
			ListDomain<OADutyList> listDomain = new ListDomain<OADutyList>();
			listDomain.setTotal(0l);
			listDomain.setRows(new ArrayList<OADutyList>());
			return listDomain;
		}
		
		String sql = dicService.getSqlByBizcode("user_jobrole_list");
		
		String whereCorse=String.format(("where deptid = %1$d"),deptid);
		sql = String.format(sql, whereCorse);		
		
		return super.getPagedListDomain(page,rows,sql);
	}
	
	
	
	@RequestMapping("/getList.action")
	public List<OADutyList> getList(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="deptid",required=false) Integer deptid,
			@RequestParam(value="sort",required=false) String sort){		
		
		StringBuilder sb = new StringBuilder();
		String sql = "";
		if(deptid!=null){
			String whereCorse=String.format(sb.append("\"deptid\" = %1$d").toString(),deptid);
			sql = String.format(this.TW_SQL, this.tableName,whereCorse);
		}else{
			sql = String.format(this.T_SQL, this.tableName);
		}
		
		return super.getPagedList(page,rows,sql);
	}
	
	
	@RequestMapping("/getDutyListDomain.action")
	public ListDomain<OADutyList> getRoomListDomain(
			@RequestBody(required=false) String body){	
		ListDomain<OADutyList> domains= super.getListDomainByConditionAndOrder(body, typeMap, orderMap,true);
		return domains;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/getDutyList.action")
	public List<OADutyList> getRoomList(@RequestBody(required=false) String body){	
		Map<String,Object> bodyMap = this.getBodyParam(body);
		String userid = shiroSecurityService.getCurrentUserIdentify();
		addUsridConditionToMap(userid,bodyMap,"apply_id");
		Map<String,String> paramMap = (Map<String, String>) bodyMap.get(ConstSysParam.CONDITION_MAP_KEY);
		List<OADutyList> domains= super.getListByConditionAndOrder(paramMap, typeMap, orderMap,true,false);
		return domains;
	}
	
	
	@SuppressWarnings({ "unchecked", "unused" })
	@RequestMapping("/getUserList.action")
	public List<OADutyList> getUserList(@RequestBody(required=false) String body){	
		Map<String,Object> bodyMap = this.getBodyParam(body);
		String userid = shiroSecurityService.getCurrentUserIdentify();
		Map<String,String> paramMap = (Map<String, String>) bodyMap.get(ConstSysParam.CONDITION_MAP_KEY);
		List<OADutyList> domains= super.getListByConditionAndOrder(paramMap, typeMap, orderMap,true,false);
		return domains;
	}
	
	
	/**
	 * 根据用户id查找用户信息
	 * @param id	用户id
	 * @return 用户信息记录 
	 */
	@RequestMapping("/getOne.action")
	public OADutyList getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOneByIdentify("id", id.toString());		
	}
	
	/**
	 * 根据用户唯一编码查找用户信息
	 * @param identify	用户唯一编码
	 * @return 用户信息记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public OADutyList getOneByIdentify(@RequestParam(value="identify",required=false) String identify){	
		return super.getOneByIdentify(identify);
	}
	
	/**
	 * 根据部门id查找用户集合
	 * @param page	页码
	 * @param rows	行数
	 * @param deptid	部门编码
	 * @return	用户数据集
	 */
	@RequestMapping("/getUserByDeptid.action")
	public ListDomain<OADutyList> getOADutyListByDept(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="deptid",required=false) Long deptid){
		
		
		String view_department = ConstSysParam.VIEW_PREFIX + ConstSysParam.MODULE_DEPARTMENT;
		
		String sql = " SELECT u.* FROM %1$s u LEFT JOIN %2$s d ON u.deptid=d.deptid AND d.deptid=%3$d" ;
		sql= String.format(sql, ConstSysParam.VIEW_USER, view_department,deptid);		
		
		return super.getPagedListDomain(page, rows, sql);
	}
	
	/**
	 * 获取用户密码
	 * @param yhid
	 * @return
	 */
	@RequestMapping("/getUserPassword.action")
	public UserPassword getUserPassword(@RequestParam(value="yhid",required=false)String yhid){
		
		String sql = "select * from %1$s where yhdm = '%2$s'";
		sql = String.format(sql, "V_MH_XTYH",yhid);
		Query qry = entityManager.createNativeQuery(sql); 
		return (UserPassword) super.reflectObjectMap(UserPassword.class, super.getSingalQueryResult(qry), null);
	}
}
